<template>
  <div class="app-container">
    <el-row v-if="userInfo.userType === 0" :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:apply:detail', 'system:apply:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="应用编码" align="center" prop="applicationKey" />
      <el-table-column label="应用图标" align="center" prop="applicationLogo">
        <template slot-scope="{ row }">
          <image-preview
            v-if="row.applicationLogo"
            :src="row.applicationLogo"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="应用名称"
        align="center"
        prop="applicationName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="应用简介"
        align="center"
        prop="applicationDesc"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="应用类型" align="center" prop="applicationType">
        <template slot-scope="{ row }">
          {{ row.applicationType === 0 ? "平台应用" : "" }}
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="applicationStatus"
        width="150px"
      >
        <template slot-scope="{ row }">
          <el-switch
            slot="reference"
            v-model="row.applicationStatus"
            :active-value="1"
            active-text="启用"
            :inactive-value="0"
            inactive-text="禁用"
            :disabled="userInfo.userType !== 0"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:apply:detail', 'system:apply:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="goOpeningDetails(scope.row)"
            v-hasPermi="['system:apply:openingDetails']"
            >开通详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getApplyList,
  updateApply,
} from "@/api/system/apply.js";

export default {
  name: "Apply",
  dicts: [],
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo;
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getApplyList(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      })
    },
    // 修改状态
    changeStatus(row) {
      const that = this;
      if(row.applicationStatus === 1){
        updateApply(row).then(() => {
          that.$message.success('状态修改成功');
        }).catch(() => {
          row.applicationStatus = row.applicationStatus ? 0 : 1; // 修改失败,复位
        })
      }else{
        that.$confirm('应用禁用后，平台所有机构均无法使用该应用，请谨慎操作。是否确定要禁用该应用？', '提示', {
          distinguishCancelAndClose: true,
          type: 'warning',
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          updateApply(row).then(() => {
            that.$message.success('状态修改成功');
          }).catch(() => {
            row.applicationStatus = row.applicationStatus ? 0 : 1; // 修改失败,复位
          })
        }).catch(() => {
          row.applicationStatus = row.applicationStatus ? 0 : 1; // 修改失败,复位
        });
      }
    },
    handleAdd() {
      this.$router.push({ path: '/system/applyDetail/index' });
    },
    handleUpdate(row) {
      this.$router.push({ path: `/system/applyDetail/index?applicationId=${row.applicationId}` });
    },
    goOpeningDetails(row) {
      this.$router.push({ path: `/system/openingDetails/index?applicationId=${row.applicationId}` });
    }
  },
};
</script>
